//
//  CrowdfundingViewController.m
//  ioschishan
//
//  Created by 马田成 on 2017/7/10.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "CrowdfundingViewController.h"
#import "SDCycleScrollView.h"

#import "ZJScrollPageView.h"
#import "CrowdFundingAnotherViewController.h"
#import "GYZChooseCityController.h"
#import "LaunchCrowdfundingViewController.h"

@interface CrowdfundingViewController ()<SDCycleScrollViewDelegate,ZJScrollPageViewDelegate,GYZChooseCityDelegate>
{

    UIButton *chooseCityBtn;

}


@property (weak, nonatomic) ZJContentView *contentView;
@property (weak, nonatomic) ZJScrollSegmentView *segmentView;
@property(strong, nonatomic)NSArray<UIViewController<ZJScrollPageViewChildVcDelegate> *> *childVcs;


@property(strong, nonatomic)NSMutableArray *titles;
@property (nonatomic, strong) UILabel *label;


@end

@implementation CrowdfundingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"众筹"];
    
    [self leftNavHide];
    [self creationTitle];
    
    // Do any additional setup after loading the view.
}
- (void)creationTitle{
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    style.scrollTitle = YES;
    style.autoAdjustTitlesWidth = YES;
    style.adjustCoverOrLineWidth = YES;
    style.selectedTitleColor = [UIColor colorWithRed:6 / 255.0f green:6 / 255.0f blue:6 / 255.0f alpha:1];
    style.scrollLineColor = [UIColor colorWithRed:93 / 255.0f green:202 / 255.0f blue:53 / 255.0f alpha:1];
    style.segmentHeight = 39;
        self.titles = @[@"全部",
                        @"老师",
                        @"学校",
                        @"孩子",
                        ].mutableCopy;
    
    // 初始化
    ZJScrollPageView *scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64.0) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    
    [scrollPageView setSelectedIndex:self.indexID animated:YES];
    scrollPageView.contentView.scrollView.scrollEnabled = YES;
    [self.view addSubview:scrollPageView];
    
    UILabel *labelOfOne = [[UILabel alloc] init];
    [self.view addSubview:labelOfOne];
    labelOfOne.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1);
    labelOfOne.backgroundColor = getThreadColor();
    
    
    chooseCityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseCityBtn.frame = CGRectMake(0, 0, screenScale(40), screenScale(20));
    [chooseCityBtn addTarget:self action:@selector(leftNavItemClick) forControlEvents:UIControlEventTouchUpInside];
   
    self.label = [[UILabel alloc] init];
    [chooseCityBtn addSubview:self.label];
    self.label.text = @"选择城市";
    self.label.font = [UIFont systemFontOfSize:16];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(chooseCityBtn);
        make.left.equalTo(chooseCityBtn);
        
        
    }];
    
    UIImageView *img = [[UIImageView alloc] init];
    [chooseCityBtn addSubview:img];
    img.image = [UIImage imageNamed:@"arrow_down"];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.label.mas_centerY);
        make.left.equalTo(self.label.mas_right);
        
        
    }];
    
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:chooseCityBtn];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    
    
    UIButton *searchBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(40), screenScale(40))];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    
    [searchBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
 
    [searchBtn setImage:[UIImage imageNamed:@"launch_fund_icon"] forState:UIControlStateNormal];
    
    
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:searchBtn];
    self.navigationItem.rightBarButtonItem = rightItme;
    

}

#pragma mark ----------------ZJScrollPageViewDelegate---------------------
- (NSInteger)numberOfChildViewControllers {
    
    return self.titles.count;
    
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    CrowdFundingAnotherViewController <ZJScrollPageViewChildVcDelegate> *childVc = (CrowdFundingAnotherViewController *)reuseViewController;
    
    if (!childVc) {
        
        childVc = [[CrowdFundingAnotherViewController alloc] init];

        if ([[self.titles objectAtIndex:index] isEqualToString:@"全部"]) {
            childVc.orderStatus = @"0";
        }else if([[self.titles objectAtIndex:index] isEqualToString:@"老师"]){
        
            childVc.orderStatus = @"1";
        }else if([[self.titles objectAtIndex:index] isEqualToString:@"学校"]){
            
            childVc.orderStatus = @"2";
        }else if([[self.titles objectAtIndex:index] isEqualToString:@"孩子"]){
            
            childVc.orderStatus = @"3";
        }
        
        NSLog(@"AAAAAAAAAAAAAAA%@", childVc.orderStatus);
        
        
    }
    
    return childVc;
}
//位置选择
- (void)leftNavItemClick{

    GYZChooseCityController *cityPickerVC = [[GYZChooseCityController alloc] init];
    [cityPickerVC setDelegate:self];
    
    
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:cityPickerVC] animated:YES completion:^{
        
    }];


}

#pragma mark - GYZCityPickerDelegate
- (void) cityPickerController:(GYZChooseCityController *)chooseCityController didSelectCity:(GYZCity *)city
{
    self.label.text = city.cityName;
    
    [chooseCityController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void) cityPickerControllerDidCancel:(GYZChooseCityController *)chooseCityController
{
    [chooseCityController dismissViewControllerAnimated:YES completion:^{
        
    }];
}




//右边❤️
- (void)saveBtnClick{

    LaunchCrowdfundingViewController *vc = [[LaunchCrowdfundingViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
